package ph.com.gs3.formalistics.model.dao;

import java.util.ArrayList;
import java.util.List;

import ph.com.gs3.formalistics.model.tables.DocumentsTable;
import ph.com.gs3.formalistics.model.tables.FLDataTable;
import ph.com.gs3.formalistics.model.tables.FormsTable;
import ph.com.gs3.formalistics.model.tables.UserAccountTable;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class FLDatabaseHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "formalistics.db";
	private static final int DATABASE_VERSION = 1;

	private Context context;

	List<FLDataTable> dataTables;

	public FLDatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.context = context;
	}

	public Context getContext() {
		return context;
	}

	/**
	 * Lazy loaded method for initializing data tables. This initializes the data tables
	 * only once in the application.
	 */
	private void initializeDataTables() {
		if (dataTables == null) {
			dataTables = new ArrayList<FLDataTable>();

			// add other defined data tables here
			dataTables.add(new UserAccountTable());
			dataTables.add(new FormsTable());
			dataTables.add(new DocumentsTable());

		}
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		initializeDataTables();

		for (FLDataTable dataTable : dataTables) {
			dataTable.onCreate(db);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		initializeDataTables();

		for (FLDataTable dataTable : dataTables) {
			dataTable.onUpgrade(db, oldVersion, newVersion);
		}
	}

}
